<template>
  <div class="app-container">
    <el-row>
      <el-col :span="8" v-show="editFlag" class="tl-left-tree">
        <!-- 树 -->
        <el-input v-model="queryTreeParam.qry_name" placeholder="根据项目名称筛选" @keyup.enter.native="filterTree" ref="treeSearch" v-if="editFlag">
          <el-button current-node-key="" slot="append" icon="el-icon-search" @click="filterTree"></el-button>
        </el-input>
        <el-scrollbar ref="treeScroll" :style="{ height: treeHeight + 'px' }" resizable-key="treeHeight" :resizable-reduce="20" :resizable-refs="['treeSearch']">
          <el-tree class="tl-tree" ref="tree" :data="treeData" node-key="id" :default-expand-all="true" highlight-current :expand-on-click-node="false" @node-click="doTreeQuery" :filter-node-method="filterTreeNode"></el-tree>
        </el-scrollbar>
      </el-col>
      <el-col :span="editFlag ? 16 : 24">
        <el-row>
          <!-- 查询 -->
          <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" v-if="isHeaderDisplay">
            <template slot="search">
              <el-form-item prop="qry_begindocdate">
                <label>日期范围</label>
                <el-date-picker v-model="queryParam.qry_startdate" type="date" style="width:150px" value-format="yyyy-MM-dd" placeholder="开始时间" />至
                <el-date-picker v-model="queryParam.qry_enddate" type="date" style="width:150px" value-format="yyyy-MM-dd" placeholder="结束时间" />
              </el-form-item>
            </template>
          </tl-list-search>
        </el-row>
        <el-card>
          <el-row>
            <!-- 工具栏 -->
            <tl-list-toolbar ref="toolbar" v-if="editFlag" :isDisplayAdd="false" @add="openAddDialog('editForm')" :isDisplayExport="false" @exportExcel="doExportExcel" :isDisplayDeleteBatch="false" @deleteBatch="doDeleteBatch" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :disableDeleteBatch="selectedRowKeys.length <= 0"> </tl-list-toolbar>
          </el-row>
          <el-row ref="selector">
            <!-- 列表选择 -->
            <div class="table-alert-div">
              <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
                <template slot="title">
                  已选择&nbsp;<a style="font-weight: bold;">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
                  <a style="font-weight: bold;margin-left: 24px" @click="doClearSelected">全部清空</a>
                </template>
              </el-alert>
            </div>
          </el-row>
          <el-row>
            <el-table ref="mainTable" class="tl-table" :height="tableHeight" resizable-key="tableHeight" :resizable-reduce="42" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" style="width: 100%;" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="id" border fit :row-class-name="doChangeRowClassName" :default-sort="{ prop: 'code', order: 'ascending' }">
              <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
              <!-- <el-table-column prop="dispprojectid" label="项目" width="100px" sortable="custom" sort-by="projectid" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column> -->
              <el-table-column v-if="editFlag" prop="projectcode" label="项目编号" width="150px" sortable="custom" sort-by="projectcode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="calendardate" label="日期" width="100px" sortable="custom" sort-by="calendardate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="dispcalendartype" label="日历类型" width="120px" sortable="custom" sort-by="calendartype" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column prop="description" label="备注" min-width="80px" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
              <!-- <el-table-column prop="enabled" label="有效" width="80px" sortable="custom" sort-by="enabled" align="center" header-align="center">
                                <template slot-scope="scope">
                                    <el-tag v-if="scope.row.enabled === 1" type="success">是</el-tag>
                                    <el-tag v-else type="danger">否</el-tag>
                                </template>
                            </el-table-column> -->
              <el-table-column label="操作" width="140px" v-slot="scope" fixed="right">
                <el-button type="text" icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-button>
                <el-button type="text" icon="el-icon-edit" @click="openEditDialog('editForm', scope.row.id)" :disabled="!editFlag">编辑</el-button>
                <!-- <el-button type="text" icon="el-icon-delete" @click="doDelete(scope.row.id)">删除</el-button> -->
                <!-- <el-dropdown trigger="hover">
                                    <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                                    <el-dropdown-menu>
                                        <el-dropdown-item @click.native="openAddDialog('editForm', scope.row)">添加下级</el-dropdown-item>
                                        <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
                                    </el-dropdown-menu>
                                </el-dropdown> -->
              </el-table-column>
            </el-table>
          </el-row>
          <!-- 列表分页 -->
          <el-row class="pagebar" ref="pager">
            <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
          </el-row>
        </el-card>
      </el-col>
      <!-- 编辑界面 -->
      <tl-edit ref="editForm" @refresh="doAllRefresh" :projectId="projectId" :editFlag="editFlag"></tl-edit>
    </el-row>
  </div>
</template>
<script>
/** mixins */
import { mixin_tree_list } from '@tapui/mixins/mixin_tree_list'
import { TreeQueryMode } from '@tapui/const/tapConst'
/** 子组件 */
import tlEdit from './edit'
import { formatDate } from '@tapui/utils/date'
export default {
  name: 'pmCalendar',
  mixins: [mixin_tree_list],
  components: { tlEdit },
  data() {
    return {
      urlPrefix: '/pm/calendar/',
      url: {
        // queryCodeList: '/pm/calendar/dataCodeList'
        queryTree: '/pm/calendar/queryTreeList'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'calendardate',
        sord: 'asc'
      },
      // 数列表关联字段
      treeQueryKey: 'qry_projectid',
      queryParam: {
        qry_projectid: this.projectId
      },
      // 当前页面的编辑状态
      editFlag: true,
      reduceHeight: 345
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    // 模块是否可以编辑
    moduleEditFlag: {
      type: Number,
      default: function() {
        return 1
      }
    },
    // 当前登录人角色 PM--项目经理 PP--项目参数人 OTHER--其他
    currentRole: {
      type: String,
      default: function() {
        return 'PM'
      }
    }
  },
  methods: {
    doTreeQuery(treeNode) {
      if (!this.editFlag) {
        this.selectTreeNode.id = this.projectId
        // 查询方式为树节点查询
        this.treeQueryMode = TreeQueryMode.TreeDlick
        this.pageInfo.currentPage = 1
        let params = this.getQueryParams()
        this.doListQuery(params)
      } else {
        // 查询方式为树节点查询
        this.treeQueryMode = TreeQueryMode.TreeDlick
        this.selectTreeNode = treeNode
        this.pageInfo.currentPage = 1
        let params = this.getQueryParams()
        this.doListQuery(params)
      }
    },
    dateTimeFormat(row, column, cellValue, index) {
      let dateValue = row[column.property]
      if (dateValue && dateValue.length > 0) {
        return formatDate(new Date(dateValue), 'yyyy-MM-dd')
      } else {
        return ''
      }
    },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      if (this.rowDbClickActionType === 'edit' && this.editFlag) {
        this.openEditDialog('editForm', row.id)
      } else {
        this.openViewDialog('editForm', row.id)
      }
    },
    initParam() {
      this.editFlag = this.moduleEditFlag === 1 && this.currentRole === 'PM'
      this.reduceHeight = this.editFlag ? 134 : 345
    },
    searchList(projectId) {
      if (!this.editFlag) {
        this.queryParam.qry_projectid = projectId
        this.loadData({})
      }
    }
  },
  watch: {
    projectId(newValue, oldValue) {
      this.searchList(newValue)
    }
  },
  mounted() {
    this.initParam()
  }
}
</script>
<style lang="scss" scoped></style>

